// pages/common/calendarOne/calendarOne.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '选择时间', // 导航栏 中间的标题
      textColor: '#FFFFFF', // 标题颜色
      bgColor: '#F76A1A', // 导航栏背景颜色
      isloading: false,
    },
    // 此页面 页面内容距最顶部的距离
    height: app.globalData.systeminfo.statusBarHeight * 2 + 20,
    hasEmptyGrid: false,
    cur_year: '',
    cur_month: '',
    seleTime: '',
    path_url: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setNowDate();
    this.setData({
      path_url: options.path_url
    })
  },
  formatting(cur_day){
    if(cur_day < 9){
      var arr=['00','01','02','03','04','05','06','07','08','09'];
      console.log("cur_day",arr[cur_day+1])
      return arr[cur_day+1] 
    }else{
      return cur_day+1
    }
  },
  dateSelectAction: function (e) {
    var cur_day = e.currentTarget.dataset.idx;
    var cur_day_zero = this.formatting(cur_day)
    this.setData({
      todayIndex: cur_day,
      seleTime: `${this.data.cur_year}-${this.data.cur_month}-${cur_day_zero}`
    })
    console.log(`点击的日期:${this.data.cur_year}年${this.data.cur_month}月${cur_day_zero}日`);
  },

 setNowDate: function () {
   const date = new Date();
   const cur_year = date.getFullYear();
   const cur_month = date.getMonth() + 1;
   const todayIndex = date.getDate() - 1;
   console.log(`日期：${todayIndex}`)
   const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
   this.calculateEmptyGrids(cur_year, cur_month);
   this.calculateDays(cur_year, cur_month);
   this.setData({
     cur_year: cur_year,
     cur_month: cur_month,
     todayIndex: todayIndex,
     weeks_ch,
     
   })
 },

 getThisMonthDays(year, month) {
   return new Date(year, month, 0).getDate();
 },
 getFirstDayOfWeek(year, month) {
   return new Date(Date.UTC(year, month - 1, 1)).getDay();
 },
 calculateEmptyGrids(year, month) {
   const firstDayOfWeek = this.getFirstDayOfWeek(year, month);
   let empytGrids = [];
   if (firstDayOfWeek > 0) {
     for (let i = 0; i < firstDayOfWeek; i++) {
       empytGrids.push(i);
     }
     this.setData({
       hasEmptyGrid: true,
       empytGrids
     });
   } else {
     this.setData({
       hasEmptyGrid: false,
       empytGrids: []
     });
   }
 },
 calculateDays(year, month) {
   let days = [];

   const thisMonthDays = this.getThisMonthDays(year, month);

   for (let i = 1; i <= thisMonthDays; i++) {
     days.push(i);
   }

   this.setData({
     days
   });
 },
 handleCalendar(e) {
   const handle = e.currentTarget.dataset.handle;
   const cur_year = this.data.cur_year;
   const cur_month = this.data.cur_month;
   if (handle === 'prev') {
     let newMonth = cur_month - 1;
     let newYear = cur_year;
     if (newMonth < 1) {
       newYear = cur_year - 1;
       newMonth = 12;
     }

     this.calculateDays(newYear, newMonth);
     this.calculateEmptyGrids(newYear, newMonth);

     this.setData({
       cur_year: newYear,
       cur_month: newMonth
     })

   } else {
     let newMonth = cur_month + 1;
     let newYear = cur_year;
     if (newMonth > 12) {
       newYear = cur_year + 1;
       newMonth = 1;
     }

     this.calculateDays(newYear, newMonth);
     this.calculateEmptyGrids(newYear, newMonth);

     this.setData({
       cur_year: newYear,
       cur_month: newMonth
     })
   }
 },
  confirmTime(){
    var seleTime = this.data.seleTime
    this.handleClick(seleTime);
  },
  handleClick(seleTime){
    console.log("seleTime",seleTime)
    let pages = getCurrentPages();   //获取小程序页面栈
    let beforePage = pages[pages.length -2];  //获取上个页面的实例对象
    console.log("过来的路径",beforePage)
    beforePage.setData({      //直接修改上个页面的数据（可通过这种方式直接传递参数）
      seleTime: seleTime
    })
    beforePage.getstatisticsl();
    wx.navigateBack({         //返回上一页  
      delta:1
    })
  },
  onReady: function () {
    this.smallLay = this.selectComponent("#smallLay"); 
  },
  showDialog() {
    this.smallLay.showSmallLay();
  },
})